<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset = "UTF-8">
    <script src="{{url_for('static',filename='js/jquery.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/strategySystemThreadMonitor.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/strategySystemActive.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/webThreadMonitor.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/tradeSystemThreadMonitor.js')}}?{{version}}"></script>
    <script src="{{url_for('static',filename='js/alert.js')}}"></script>
    <script src="{{url_for('static',filename='js/bootstrap.min.js')}}"></script>
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">
    <style>
    .button{
        background-color: white;
        color: black;
        border: 2px solid #4CAF50;
    }

    .button:hover {
        background-color: #4CAF50;
        color: white;
    }
    .button[disabled] {
        opacity: 0.6;
        cursor: not-allowed;
    }

    </style>
    <title>线程监控</title>
</head>
<body>
        <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">盯盘宝后台管理系统</a>
    </div>
    <div>
        <ul class="nav navbar-nav">
            <li ><a href="{{ url_for('start_web') }}">首页</a></li>
            <li ><a href="{{ url_for('permission_manage') }}">权限配置</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    管理员{{ session['admin'] if session['admin'] else "未登录" }}
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url_for("login_out") }}">退出</a></li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</nav>
<audio id="soundControl" height="0" width="0" hidden="true" controls="controls">
  <source src="/static/10151.mp3" type="audio/mp3" />
</audio>

<div align="center">

<div class="btn-group btn-group-lg" role="group" aria-label="...">


        <div class="btn-group" role="group">
        <button id ="month" type="button" class="btn btn-primary" onclick="procManage()">进程管理</button>
        </div>

    </div>
</div>
<br/>
<br/>
<table colume="center" class="table table-striped table-bordered"  style="width:100%">
    <tr>
        <td>
            <div id="strategy_system_active" style="width:80%;">
            <h3>策略系统节点存活状态</h3>
            <div style="margin:auto">

            <div id="strategySystemActiveDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
            </div>
            </div>
            </div>
        </td>
        <td>
            <div id="strategy_system_thread_monitor" style="width:80%;">

            <h3>策略系统线程监控</h3>
            <div style="margin:auto">

            <div id="strategySystemDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
            </div>
            </div>
            </div>
        </td>
        <td>
            <div align="left" id="info" style="overflow-y:auto;width:200px;height:400px;border:solid 1px #a1bece;background-color:#EEF7EA;">


            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div id="web_thread_monitor" style="width:80%" >
            <h3>web线程监控</h3>
            <div style="margin:auto">

            <div id="webDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
            </div>
            </div>
            </div>
        </td>
        <td>
            <div id="trade_system_thread_monitor" style="width:80%;">

            <h3>交易系统线程监控</h3>
            <div style="margin:auto">

            <div id="tradeSystemDataTable" class="table table-striped table-bordered" cellspacing="0" width="80%">
            </div>
            </div>
            </div>
        </td>
        <td>
            <div align="left" id="realtime_info" style="overflow-y:auto;width:200px;height:400px;border:solid 1px #a1bece;background-color:#EEF7EA;">


            </div>
        </td>
    </tr>
</table>
<script>
    function procManage() {
        window.open("./threadMonitor/procManage")
    }
     function ManageSoundControl(action)
       {
            var soundControl = document.getElementById("soundControl");
            if(action == "play")
          {
                soundControl.play();
            }
            if(action == "stop")
            {
                soundControl.pause();
            }
        }

        function formatDate(time){
            var date = new Date(time);

            var year = date.getFullYear(),
                month = date.getMonth()+1,//月份是从0开始的
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();
            var newTime = year + '-' +
                        (month < 10? '0' + month : month) + '-' +
                        (day < 10? '0' + day : day) + ' ' +
                        (hour < 10? '0' + hour : hour) + ':' +
                        (min < 10? '0' + min : min) + ':' +
                        (sec < 10? '0' + sec : sec);

            return newTime;
        }

        var strategySystemDataTable = null;
        var strategySystemActiveDataTable = null;
        var webDataTable = null;
        var tradeSystemDataTable = null;
         strategySystemDataTable = jQuery("#strategySystemDataTable").raytable({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "node_name", title: "节点名"},
            { field: "module", title: "模块"},
            { field: "proc_name", title: "进程名"},
            { field: "thread_name", title: "线程名"},
            { field: "ht", title: "线程是否正常运行"},
            { field: "reboot_proc", title: "紧急恢复"}
            ],
            pagesize: 5,
            maxPageButtons: 5,
            rowNumbers: true
        });

          webDataTable = jQuery("#webDataTable").rayWebTable({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "node_name", title: "节点名"},
            { field: "module", title: "模块"},
            { field: "proc_name", title: "进程名"},
            { field: "thread_name", title: "线程名"},
            { field: "ht", title: "线程是否正常运行"}
            {#{ field: "reboot_proc", title: "紧急恢复"}#}
            ],
            pagesize: 10,
            maxPageButtons: 5,
            rowNumbers: true
        });

       tradeSystemDataTable = jQuery("#tradeSystemDataTable").rayTradeSystemTable({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "node_name", title: "节点名"},
            { field: "module", title: "模块"},
            { field: "proc_name", title: "进程名"},
            { field: "thread_name", title: "线程名"},
            { field: "ht", title: "线程是否正常运行"}
            {#{ field: "reboot_proc", title: "紧急恢复"}#}
            ],
            pagesize: 6,
            maxPageButtons: 5,
            rowNumbers: true
        });

        strategySystemActiveDataTable = jQuery("#strategySystemActiveDataTable").rayActiveTable({
            datasource: { data: [], keyfield: 'stock_code' },
            columns: [
            { field: "proc_name", title: "进程名"},
            { field: "node1", title: "节点1状态"},
            { field: "node2", title: "节点2状态"},
            { field: "reboot_proc", title: "紧急恢复"}

            ],
            pagesize: 10,
            maxPageButtons: 5,
            rowNumbers: true
        });

         function refreshData() {
             $.ajax({
                 url:"/threadMonitorData",
                 async:true,
                 success:function (result) {
                     var obj = JSON.parse(result);
                     var strategySystemThreadList = obj.strategySystemThreadList;
                     var webThreadList = obj.webThreadList;
                     var tradeSystemThreadList = obj.tradeSystemThreadList;
                     var strategyBuyList = obj.strategyBuyList;
                     var strategySellList = obj.strategySellList;
                     var strategySystemActiveList = obj.active_result;
                     var realtimeData = obj.realtime_data;
                     strategySystemDataTable.data(strategySystemThreadList,'stock_code');
                     webDataTable.data(webThreadList,'stock_code');
                     tradeSystemDataTable.data(tradeSystemThreadList,'stock_code');
                     strategySystemActiveDataTable.data(strategySystemActiveList,'stock_code');
                     var nowTime =formatDate(new Date().getTime()).substring(11);
                     if (("09:08:00" <= nowTime & nowTime <= "11:31:00") | ("12:59:00" <= nowTime & nowTime <= "15:01:00")) {
                         for (var i = 0; i < strategySystemThreadList.length - 1; i++) {
                             if (strategySystemThreadList[i]['ht'] == 0) {
                                 if (strategySystemThreadList[i]['thread_name'] == 'sell'){
                                     if (nowTime >= '09:29:00'){
                                        ManageSoundControl("play");
                                        break;
                                     }
                                 }else{
                                     ManageSoundControl("play");
                                     break;
                                 }

                             }
                         }
                     }

                     var nowTime =formatDate(new Date().getTime()).substring(11);
                     if (("09:20:00" <= nowTime & nowTime <= "11:31:00") | ("12:59:00" <= nowTime & nowTime <= "15:01:00")){
                         for (var i =0;i<webThreadList.length-1;i++ ){
                         if (webThreadList[i]['ht'] == 0){
                             ManageSoundControl("play");
                             break;
                         }
                     }
                     }
                      if (("09:20:00" <= nowTime & nowTime <= "11:31:00") | ("12:59:00" <= nowTime & nowTime <= "15:01:00")){
                         for (var i =0;i<tradeSystemThreadList.length-1;i++ ){
                         if (tradeSystemThreadList[i]['ht'] == 0){
                             ManageSoundControl("play");
                             break;
                         }
                     }
                     }
                     var htmlStr = '<p><b>策略系统交易记录：</b></p>';
                     for (var i=0; i<strategyBuyList.length; i++){
                         var strList = strategyBuyList[i].split('#');
                         htmlStr += strList[0] + ' ' + strList[1] + '买入 ' + strList[2] + '<br />';
                     }
                     for (var i=0; i<strategySellList.length; i++){
                         var strList = strategySellList[i].split('#');
                         htmlStr += strList[0] + ' ' + strList[1] + '卖出 ' + strList[2] + '<br />';
                     }
                     $("#info").html(htmlStr);
                    var htmlStr = '<p><b>卡夫卡数据：</b></p>';
                    for (var key in realtimeData){
                        htmlStr += realtimeData[key]+ '<br />';
                    }

                     $("#realtime_info").html(htmlStr);
                 }
             })
         }
         refreshData();
         setInterval(function () {
             refreshData();
         },20000);

</script>

</body>
</html>